﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.SharePoint;
using MS.InformationManagement.Support.Constants;
using MS.InformationManagement.DAL.DocumentCenter;
using MS.InformationManagement.Support.Enums;
using MS.InformationManagement.Core.Support;
using MS.InformationManagement.DAL.DTOs;
using office = Microsoft.Office.DocumentManagement.DocumentSets;
using MS.InformationManagement.Support;

namespace MS.InformationManagement.BLL.CancelShipping
{
    public class CancelShipping
    {
        public static bool CancelShippingItem(int CurrentId, string CurrentListId, int IdDevolucion, string Motivo)
        {
            bool result = false;
            try
            {
                SPSecurity.RunWithElevatedPrivileges(delegate()
                {
                    using (SPSite currentSite = new SPSite(SPContext.Current.Site.ID))
                    {
                        using (SPWeb currentWeb = currentSite.OpenWeb())
                        {
                            currentWeb.AllowUnsafeUpdates = true;
                            currentSite.AllowUnsafeUpdates = true;

                            SPList documentsList = currentWeb.Lists[new Guid(CurrentListId)];
                            SPListItem documentCopiaListItem = documentsList.GetItemById(CurrentId);

                            if (documentCopiaListItem.ContentType.Name == GlobalConstants.CTTN_COPIAS_RADICADO)
                            {
                                documentCopiaListItem[CustomSiteColumns.FLD_COPY_STATUS_CANCEL] = true;
                                documentCopiaListItem[CustomSiteColumns.FLD_COPY_MOTIVO_RECHAZO] = Motivo;
                            }
                            else
                            {
                                documentCopiaListItem[CustomSiteColumns.FIELD_DOCUMENT_STATUS_CANCEL] = true;
                                documentCopiaListItem[CustomSiteColumns.FIELD_DOCUMENT_MOTIVO_RECHAZO] = Motivo;
                            }                           

                            //Update Logs
                            DeliveryTraceabilityEntry deliveryLogEntry = getDeliverEntity(GetEntity(IdDevolucion));
                            documentCopiaListItem.UpdateDeliveryLog(deliveryLogEntry);
                            documentCopiaListItem.Log(string.Empty, DateTime.Now, TraceableOperation.EmvioCancelado, SPContext.Current.Web.CurrentUser.LoginName, Motivo);


                            documentCopiaListItem.Update();
                            currentSite.AllowUnsafeUpdates = false;
                            currentWeb.AllowUnsafeUpdates = false;
                        }
                    }
                    result = true;
                }); 
            }
            catch (Exception ex)
            {
                throw ex;
            }

            return result;
        }


        #region Private Methods

        private static DeliveryTraceabilityEntry getDeliverEntity(Devolucion currentEntity)
        {
            DeliveryTraceabilityEntry logEntry = new DeliveryTraceabilityEntry();
            logEntry.Address = currentEntity.Dirección;
            //logEntry.City = currentEntity.;
            logEntry.DateTime = DateTime.Now;
            logEntry.To = currentEntity.RemitenteDestinatario;
            logEntry.Operation = DeliveryOperations.Recibido.ToString();
            logEntry.User = SPContext.Current.Web.CurrentUser.LoginName;
            return logEntry;
        }

        private static Devolucion GetEntity(int id)
        {
            using (var dc = new DCDocumentCenterDataContext(SPContext.Current.Web.Url))
            {
                var entity = (from item in dc.Devoluciones
                        where item.Id == id
                        select item).FirstOrDefault();

                entity.EnvíoActivo = false;
                dc.SubmitChanges();

                return entity;
            }
        }

        #endregion
    }
}
